﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Data.OleDb;
using System.IO;
using System.Data;
using SDD;
using NPOI.HSSF.UserModel;

namespace SDD
{
    class FundFlow
    {

        public string url1 = "http://estockweb.standardchartered.com.tw/z/zb/zba/zba.asp.htm";

        private string FullFileName = Program.FileDir + "類股資金流向.xls";

        public string dataDay = DateTime.Today.Year.ToString() + DateTime.Today.Month.ToString("00") + DateTime.Today.Day.ToString("00");
        public string msg = "";
        public void AggregateData()
        {
            try
            {
                analysisHTML(url1);
            }
            catch
            {
                Console.WriteLine("類股資金流向 資料擷取失敗!!");
            }
        }
        /// <summary>
        /// 解析HTML new
        /// </summary>
        /// <param name="url"></param>
        /// <param name="ds"></param>
        public void analysisHTML(string url)
        {
            string context = new cHttp().dopost(url);
            WebBrowser wbr = new WebBrowser();
            wbr.Navigate("");

            wbr.ScriptErrorsSuppressed = true;
            wbr.Document.Write("<script type='text/javascript'>window.onerror = function(){};</script>" + context);

            HtmlElementCollection tb = wbr.Document.GetElementsByTagName("TABLE");
            HtmlElementCollection tr = tb[2].GetElementsByTagName("TR");
            string[] dataday = tr[0].InnerText.Substring(tr[0].InnerText.IndexOf(":") + 2).Split('/');

            NPOIComFunction xls = new NPOIComFunction(FullFileName);
            HSSFSheet sheet = null;

            /////////////////////
            //建立工作表 - 上市資金流向表
            if (xls.Workbook.GetSheetIndex("上市資金流向表") > -1)
                sheet = xls.Workbook.GetSheet("上市資金流向表");
            else
            {
                //新的資料表  寫入類股名稱
                sheet = xls.Workbook.CreateSheet("上市資金流向表");
                sheet.CreateRow(0).CreateCell(0).SetCellValue("日期");
                // col = 0 為日期
                int col = 1;
                try
                {
                    for (int i = 2; i < 10; i++)
                    {
                        for (int j = 0; j < 7; j += 2)
                        {
                            sheet.CreateRow(0).CreateCell(col).SetCellValue(tr[i].GetElementsByTagName("TD")[j].InnerText);
                            col++;
                        }
                    }
                }
                catch { }
            } // end if

            /////////////////////
            //建立工作表 - 上櫃資金流向表
            HSSFSheet sheetB = null;
            if (xls.Workbook.GetSheetIndex("上櫃資金流向表") > -1)
                sheetB = xls.Workbook.GetSheet("上櫃資金流向表");
            else
            {
                //新的資料表  寫入類股名稱
                sheetB = xls.Workbook.CreateSheet("上櫃資金流向表");
                sheetB.CreateRow(0).CreateCell(0).SetCellValue("日期");
                // col = 0 為日期
                int col = 1;
                try
                {
                    for (int i = 12; i < 16; i++)
                    {
                        for (int j = 0; j < 7; j += 2)
                        {
                            sheetB.CreateRow(0).CreateCell(col).SetCellValue(tr[i].GetElementsByTagName("TD")[j].InnerText);
                            col++;
                        }
                    }
                }
                catch { }
            } // end if


            //////////////////////////
            //寫入類股資金流向

            //上市資金流向表
            int rowid = 0;
            try
            {
                //找出從哪裡開始寫
                rowid = sheet.LastRowNum + 1;
                for (int k = sheet.LastRowNum; k > 0; k--)
                {
                    rowid = sheet.GetRow(k).GetCell(0).StringCellValue.Equals(dataday[0] + "/" + dataday[1]) ? k : rowid;
                }
                // col = 0 為日期
                sheet.CreateRow(rowid).CreateCell(0).SetCellValue(dataday[0] + "/" + dataday[1]);
                int col = 1;
                for (int i = 2; i < 10; i++)
                {
                    for (int j = 1; j < 8; j += 2)
                    {
                        string data = tr[i].GetElementsByTagName("TD")[j].InnerText.Replace("%", "");
                        sheet.CreateRow(rowid).CreateCell(col).SetCellValue(data);
                        col++;
                    }
                }
            }
            catch { }

            //上櫃資金流向表
            try
            {
                //找出從哪裡開始寫
                rowid = sheetB.LastRowNum + 1;
                for (int k = sheetB.LastRowNum; k > 0; k--)
                {
                    rowid = sheetB.GetRow(k).GetCell(0).StringCellValue.Equals(dataday[0] + "/" + dataday[1]) ? k : rowid;
                }
                // col = 0 為日期
                sheetB.CreateRow(rowid).CreateCell(0).SetCellValue(dataday[0] + "/" + dataday[1]);
                int col = 1;
                for (int i = 12; i < 16; i++)
                {
                    for (int j = 1; j < 8; j += 2)
                    {
                        string data = tr[i].GetElementsByTagName("TD")[j].InnerText.Replace("%", "");
                        sheetB.CreateRow(rowid).CreateCell(col).SetCellValue(data);
                        col++;
                    }
                }
            }
            catch { }

            //// 寫入實體檔案
            xls.WriteToFile();
        }

    }
}
